﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class RolesService : IRolesService
    {
        private IRolesManager _ir;
        public RolesService(IRolesManager ir)
        {
            _ir = ir;
        }

        public async Task<int> AddUsersAsync(string title)
        {
            return await _ir.AddAsync(new Model.Roles 
            {
                Title = title
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _ir.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ir.DeleteAsync(data);
        }

        public async Task<List<RolesDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _ir.GetChooseAsync(rd => rd.DeleteId == deleteId);
            return data == null ? new List<RolesDto>() : data.Select(rd => new RolesDto 
            {
                Id = rd.Id,
                Title = rd.Title,
                DeleteId = deleteId,
                CreateTime = rd.CreateTime,
                UpdateTime = rd.UpdateTime
            }).ToList();
        }

        public async Task<RolesDto> GetByIdAsync(int id)
        {
            var data = await _ir.GetById(id);
            return data == null ? new RolesDto() : new RolesDto 
            {
                Id = data.Id,
                Title = data.Title,
                DeleteId = data.DeleteId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<RolesDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _ir.GetChooseAsync(rd => rd.Title.Contains(title) && rd.DeleteId == deleteId);
            return data == null ? new List<RolesDto>() : data.Select(rd => new RolesDto 
            {
                Id = rd.Id,
                Title = rd.Title,
                DeleteId = rd.DeleteId,
                CreateTime = rd.CreateTime,
                UpdateTime = rd.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ir.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ir.UpdateAsync(data); 
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ir.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ir.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, string title)
        {
            var data = await _ir.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.UpdateTime = DateTime.Now;
            return await _ir.UpdateAsync(data);
        }
    }
}
